package com.calculator;

import com.calculator.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.NumberFormat;
import android.util.Log;

public class ACalculator extends Activity {
	public static final String tag = "Calculator";
    /** Called when the activity is first created. */	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final EditText priceEdit = (EditText) findViewById(R.id.priceEditText);
        final TextView resultText = (TextView) findViewById(R.id.resultTextView);
        
        final Button buttonCalculate = (Button) findViewById(R.id.calculateButton);
        buttonCalculate.setOnClickListener(new Button.OnClickListener()
        {
         public void onClick(View v)
         {
        	 try
        	 {
        	   Log.i(tag, "onClick invoked");
        	   
        	   String price = priceEdit.getText().toString();
        	   
        	   Log.i(tag,"price is [" + price + "]");
        	   String result = "";
        	   
        	   if (price.indexOf("$") == -1)
        	   {
        	    price = "$" + price;
        	   }
        	   
        	   float fp = 0.0F;
        	   
        	   NumberFormat nf = java.text.NumberFormat.getCurrencyInstance();
        	   
        	   fp = nf.parse(price).floatValue();
        	   fp*=1.2;
        	   
        	   Log.i(tag,"Total Meal Price (unformatted) is [" + fp + "]");
        	   
        	   result = "Full Price, Including 20% Tip: " + nf.format(fp);
        	   
        	   resultText.setText(result);
        	   
        	   Log.i(tag,"onClick complete");
        	 }
        	 catch(Exception ex)
        	 {
        		 Log.i(tag,ex.getMessage());
        		 resultText.setText("Failed to calculate");
        	 }
         }
        });
    }
}